package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.ClassifierTemplateParameter;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.TemplateParameter;
import org.eclipse.uml2.uml.TemplateSignature;
import org.eclipse.uml2.uml.UMLPackage;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.preference.ClassDiagramCommonPreferenceConstants;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.SemanticModelBridge;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete an {@link TemplateParameter}}.
 * @author Arnaud Cuccuru
 */
public class NodeClassifierTemplateParameterHelper extends NodeHelper {
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphElement(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.draw2d.geometry.Point, org.eclipse.draw2d.geometry.Dimension)
	 */
	@Override
	public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {
		Debug.debug(this,"Call NodeOperationFactory.createGraphElement()", channel); //$NON-NLS-1$

		// creates the graph element corresponding to the Operation
		GraphNode graphNode = super.createGraphElement(umlChild, parent, location, size);
		
		// finally, adds the Operation graph element to its parent graph element
		parent.getContained().add(graphNode);

		return graphNode;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.INodeFactory#createGraphNode(org.eclipse.uml2.uml.NamedElement, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	@Override
	public ArrayList createGraphNode(Element umlElement, Diagram diagram) {
		return new ArrayList();
	}
	
/**
 * {@inheritDoc}
 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		
		if (!(parent instanceof Diagram)) {//the parent must be a graphNode
			SemanticModelBridge toto = parent.getSemanticModel() ;
			if(parent.getSemanticModel()==null){ // the parent must a Feature
				GraphElement gp=parent.getContainer();//look for GreatParent

				if (gp!=null){
					if (!(gp instanceof Diagram)) {//great parent is not a diagram
						if(gp.getSemanticModel()!=null){ //it is linked to an UML element
							Element elt= ((Uml1SemanticModelBridge)gp.getSemanticModel()).getElement();

							if ((elt instanceof TemplateSignature) && (gp.getContained().get(0)==parent)){ //this is the first container of a classifier.
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}
	
	///////////////////////////////////////////////////////////////////////////
	// UML Management
	///////////////////////////////////////////////////////////////////////////
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.umlElement)
	 */
	@Override
	public org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"Call NodeTemplateParameterFactory.createElement()", channel); //$NON-NLS-1$
		
		org.eclipse.uml2.uml.ClassifierTemplateParameter templateParameter=null;
		templateParameter=(ClassifierTemplateParameter)((org.eclipse.uml2.uml.TemplateSignature)umlParent).createOwnedParameter(UMLPackage.eINSTANCE.getClassifierTemplateParameter()) ;
		//templateParameter.setOwner((org.eclipse.uml2.uml.TemplateSignature)umlParent) ;
		//((org.eclipse.uml2.uml.TemplateSignature)umlParent).getOwnedParameters().add(templateParameter) ;
		
//		org.eclipse.uml2.uml.Class parameteredElement = UMLFactory.eINSTANCE.createClass() ;
		
//		parameteredElement.setName("T") ;
//		parameteredElement.setOwningTemplateParameter(templateParameter) ;
		
		
		//templateParameter.setOwnedParameteredElement(parameteredElement) ;
		
		
//		try {
//		ParameterableElement pelt = templateParameter.createOwnedParameteredElement(UMLPackage.eINSTANCE.getParameterableElement()) ;
//		//templateParameter.setParameteredElement(templateParameter.getOwnedParameteredElement()) ;
//		//((NamedElement)templateParameter.getParameteredElement()).setName("T") ;
//		}
//		catch (Exception e) {
//			e.printStackTrace() ;
//		}
		return templateParameter;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof TemplateSignature) {
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof org.eclipse.uml2.uml.TemplateSignature) {
			return ((org.eclipse.uml2.uml.TemplateSignature)umlParent).getOwnedParameters().add((TemplateParameter)element) ;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof org.eclipse.uml2.uml.TemplateSignature) {
			((org.eclipse.uml2.uml.TemplateSignature)umlParent).getParameters().remove(element) ;
			return ((org.eclipse.uml2.uml.TemplateSignature)umlParent).getOwnedParameters().remove(element) ;
			
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#getBaseString()
	 */
	@Override
	public String getBaseString() {
		return "TemplateParameter_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {
		// Get the preference store
		IPreferenceStore store = PapyrusPlugin.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(ClassDiagramCommonPreferenceConstants.getPreferenceOperationBackgroundColor(store));
		graphElement.setBorderColor(ClassDiagramCommonPreferenceConstants.getPreferenceOperationBorderColor(store));
		graphElement.setFontColor(ClassDiagramCommonPreferenceConstants.getPreferenceOperationFontColor(store));
		FontData[] fontdata = ClassDiagramCommonPreferenceConstants.getPreferenceOperationFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}
}